//
//  UserCenterList.m
//  cutter
//
//  Created by mac on 16/1/5.
//  Copyright © 2016年 e360. All rights reserved.
//
#import "UITreeView.h"
#import "UserCenterList.h"

@implementation UserCenterList
-(void)viewDidLoad{
    [super viewDidLoad];
    [self headerRefresh:nil];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (isEmpty(textField.text)) {
        [self.condition setValue:nil forKey:@"keyword"];
    }else{
        [self.condition setValue:textField.text forKey:@"keyword"];
    }
    [self headerRefresh:nil];
    return YES;
}
-(NSMutableDictionary *)condition{
    if (nil == _condition) {
        [self setCondition:[NSMutableDictionary dictionary]];
    }
    return _condition;
}
//
-(void)headerRefresh:(TMRefresh*)sender{
    while ([MBProgressHUD hideHUDForView:self.view animated:NO]);
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    //
    __weak UserCenterList *this = self;
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        UITreeNode *rootNode = nil;
        if ([this.condition objectForKey:@"keyword"]) {
            id clients = [AppData userClient:this.condition user:[TMGlobal valueForKey:@"user"]];
            if (clients) {
                id root = [NSDictionary dictionaryWithObject:clients forKey:@"clients"];
                rootNode = [this makeNodesWithData:root level:0];
            }
        }else{
            NSArray *groups = [AppData userClientGroup:[TMGlobal valueForKey:@"user"]];
            if (groups) {
                for (id group in groups) {
                    id clients = [AppData userClient:group user:nil];
                    if (clients) {
                        [group setValue:clients forKey:@"clients"];
                    }
                }
                id root = [NSDictionary dictionaryWithObject:groups forKey:@"clients"];
                rootNode = [this makeNodesWithData:root level:-1];
            }
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:this.view animated:YES];
            [this expandClient:rootNode];
            [this.clientView setRootNode:rootNode];
        });
    });
}
-(void)expandClient:(UITreeNode*)root{
    for (UITreeNode *group in root.nodes) {
        if (self.client) {
            for (UITreeNode *node in group.nodes) {
                if ([node.value[@"uuid"] isEqual:self.client[@"uuid"]]) {
                    [group setExpand:YES];
                    [node setSelect:YES];
                    return;
                }
            }
        }
    }
}
-(UITreeNode*)makeNodesWithData:(id)data level:(NSInteger)level{
    static NSInteger identify = 0;
    UITreeNode *nodes=[[UITreeNode alloc] init];
    nodes.idetify=identify++;
    nodes.level = level;
    nodes.value=data;
    //子级列出
    id child = [data objectForKey:@"clients"];
    if (child) {
        for (id item in child) {
            [nodes addNode:[self makeNodesWithData:item level:level+1]];
        }
    }
    return nodes;
}
-(UITreeNode*)currentNode:(UITreeNode*)rootNode{
    if (rootNode && self.client) {
        for (UITreeNode *group in rootNode.nodes) {
            for (UITreeNode *node in group.nodes) {
                if ([node.value[@"id"] isEqual:self.client[@"id"]]) {
                    return node;
                }
            }
        }
    }
    return nil;
}
//UITreeView
-(id)treeViewTitleForHeader:(UITreeView *)treeView{
    return @"已有客户";
}
-(void)treeView:(UITreeView *)treeView expandRowAtIndex:(NSInteger)index{
    UITreeNode *node = [treeView nodeAtIndex:index];
    for (UITreeNode *item in treeView.visibleNodes) {
        [item setSelect:node == item];
    }
    if (node.level >  0) {
        if (self.onSelected) {
            self.onSelected(self, node.value);
        }
    }else{
        [self.clientView collapseAll];
        [self.clientView expand:node];
    }
}
-(UITableViewCell *)treeView:(UITreeView *)treeView cellForRowAtIndex:(NSInteger)index{
    UITreeNode *node = [treeView nodeAtIndex:index];
    if (node.level == 0) {
        UITableViewCell *cell=[treeView dequeueReusableCellWithIdentifier:@"headIdentifier"];
        [cell.imageView setImage:[UIImage imageNamed:@"icon_Folder.png"]];
        [cell.textLabel setText:[node.value objectForKey:@"groupname"]];
        return cell;
    }
    UITableViewCell *cell=[treeView dequeueReusableCellWithIdentifier:@"cellIdentifier"];
    [cell.textLabel setText:[node.value objectForKey:@"truename"]];
    return cell;
}
- (IBAction)closeTouch:(id)sender {
    if (self.onSelected) {
        self.onSelected(self, nil);
    }else{
        if (self.navigationController) {
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }
}
@end
